package com.sannuo.store.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sannuo.common.domain.DrugStore;
import com.sannuo.common.domain.vo.StoreAndUserVo;
import com.sannuo.common.result.Result;
import com.sannuo.common.result.ResultCode;
import com.sannuo.store.service.DrugStoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/drugStore")
@Api(tags = "连锁药房管理")
@CrossOrigin
public class DrugStoreController {

    @Autowired
    private DrugStoreService drugStoreService;

    @RequestMapping(value = "listStore1", method = RequestMethod.GET)
    @ApiOperation(value = "查询所有总店信息",notes = "查询所有总店信息",httpMethod = "GET")
    public Result findAllZong(Integer page , Integer limit, String searchParams) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        StoreAndUserVo storeAndUserVo = null;
        if(searchParams != null){
            //将searchParams接收到的JSON字符串转换UserVo对象
            storeAndUserVo = objectMapper.readValue(searchParams, StoreAndUserVo.class);
        }
        Page page1 = PageHelper.startPage(page, limit);
        List<DrugStore> drugStoreList = drugStoreService.findAllHeadStore(storeAndUserVo);
        PageInfo pageInfo = new PageInfo<>(drugStoreList);
        return Result.ok(ResultCode.SUCCESS, pageInfo.getTotal(), drugStoreList);
    }

    @RequestMapping(value = "listSubStore/{zongStoreCode}", method = RequestMethod.GET)
    @ApiOperation(value = "查询所有分店信息",notes = "查询所有分店信息",httpMethod = "GET")
    public Result findAllFen(Integer page , Integer limit, String searchParams, @PathVariable("zongStoreCode") int zongStoreCode) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
//        DrugStore drugStore = null;
//        if(searchParams != null){
//            //将searchParams接收到的JSON字符串转换UserVo对象
//            drugStore = objectMapper.readValue(searchParams, DrugStore.class);
//        }
        StoreAndUserVo storeAndUserVo = null;
        if(searchParams != null){
            //将searchParams接收到的JSON字符串转换UserVo对象
            storeAndUserVo = objectMapper.readValue(searchParams, StoreAndUserVo.class);
        }
        Page page1 = PageHelper.startPage(page, limit);
        List<DrugStore> drugStoreList = drugStoreService.findAllBranchStore(zongStoreCode,storeAndUserVo);
        PageInfo pageInfo = new PageInfo<>(drugStoreList);
        return Result.ok(ResultCode.SUCCESS, pageInfo.getTotal(), drugStoreList);
    }

    @RequestMapping(value = "addStore",method = RequestMethod.POST)
    @ApiOperation(value = "增加分店" , notes = "增加药店信息" ,httpMethod = "POST")
    public Result addStore(DrugStore drugStore) {
        boolean isOk = drugStoreService.save(drugStore);
        if (isOk) {
            return Result.ok(ResultCode.SUCCESS,1);
        } else {
            return Result.fail(ResultCode.FAIL,null);
        }
    }



}
